package org.tinygroup.springmvc.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.UrlPathHelper;
import org.tinygroup.springmvc.extension.RequestInstanceHolder;
import org.tinygroup.springmvc.util.WebUtil;

/**
 * 设置请求扩展名的拦截器
 * @author renhui
 *
 */
public class SetExtensionHandlerInterceptor extends HandlerInterceptorAdapter {

	private static final UrlPathHelper urlPathHelper = new UrlPathHelper();

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

		String requestUri = urlPathHelper.getRequestUri(request);
		RequestInstanceHolder.setServletWebRequest(new ServletWebRequest(
				request, response));
		RequestInstanceHolder.setExtension(WebUtil.getExtension(requestUri));
		return true;
	}

}
